package com.yxinsur.imc.web.controller.business;

import com.alibaba.fastjson.JSONObject;
import com.yxinsur.imc.business.domain.ImcCommDetails;
import com.yxinsur.imc.business.excel.BrokerTemplate;
import com.yxinsur.imc.business.excel.CommDetailsTemplate;
import com.yxinsur.imc.business.form.CommDetailsForm;
import com.yxinsur.imc.business.service.IImcCommDetailsService;
import com.yxinsur.imc.business.vo.CommDetailsSearchVo;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.domain.model.LoginUser;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.utils.ServletUtils;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * 佣金报主Controller
 *
 * @author imc
 * @date 2021-05-25
 */
@Api(value = "佣金明细接口服务",tags = {"佣金明细接口服务"})
@RestController
@RequestMapping("/business/details")
public class ImcCommDetailsController extends BaseController {
    @Autowired
    private IImcCommDetailsService imcCommDetailsService;

    /**
     * 查询佣金报主列表
     */
    @ApiOperation("佣金明细")
    @PostMapping("/list")
    public TableDataInfo list(CommDetailsForm commDetailsForm) {
        startPage();
        List<CommDetailsSearchVo> list = imcCommDetailsService.selectDetailsByBrokerInfo(commDetailsForm);
        return getDataTable(list);
    }

    /**
     * 查询佣金报主列表
     */
    @ApiOperation("佣金明细表头")
    @PostMapping("/titles/{commId}")
    public AjaxResult titles(@PathVariable("commId") Long commId) {

        List<HashMap<String, String>> titles = imcCommDetailsService.titles(commId);

        if(titles!=null){
            return AjaxResult.success(titles);
        }else{
            return AjaxResult.error("未有效配置该报表表头信息，请联系管理员");
        }
    }

    /**
     * 导出佣金报主列表
     */
    @ApiOperation("佣金明细导出")
    @Log(title = "佣金明细导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CommDetailsForm commDetailsForm) throws NoSuchFieldException, IllegalAccessException {
        List<CommDetailsTemplate> list = imcCommDetailsService.export(commDetailsForm);
        ExcelUtil<CommDetailsTemplate> util = new ExcelUtil<CommDetailsTemplate>(CommDetailsTemplate.class);
        return util.exportExcel(list, "佣金明细详情");
    }


    /**
     * 佣金主表信息导入
     */
    @ApiOperation("佣金明细导入")
    @Log(title = "佣金明细导入", businessType = BusinessType.EXPORT)
    @PostMapping("/importDetails")
    public AjaxResult importDetails(MultipartFile file,Long commId) {

        try {
            imcCommDetailsService.importDetails(file,commId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("导入失败，请检查:"+e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 获取佣金报主详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(imcCommDetailsService.selectImcCommDetailsById(id));
    }

    /**
     * 新增佣金报主
     */
    @Log(title = "新增佣金明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImcCommDetails imcCommDetails) {
        return toAjax(imcCommDetailsService.insertImcCommDetails(imcCommDetails));
    }

    /**
     * 修改佣金报主
     */
    @Log(title = "修改佣金明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImcCommDetails imcCommDetails) {
        return toAjax(imcCommDetailsService.updateImcCommDetails(imcCommDetails));
    }

    /**
     * 删除佣金报主
     */
    @Log(title = "删除佣金明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(imcCommDetailsService.deleteImcCommDetailsByIds(ids));
    }
}
